package com.stars.controller.sys;

import com.github.pagehelper.PageInfo;
import com.stars.common.annotation.Log;
import com.stars.common.annotation.UserMenuUpdateRedis;
import com.stars.common.base.BaseCode;
import com.stars.common.base.BaseMsg;
import com.stars.common.base.ResultMsg;
import com.stars.common.enums.OperationType;
import com.stars.common.signature.MD5Util;
import com.stars.common.valid.AddView;
import com.stars.common.valid.UpdateView;
import com.stars.controller.BaseController;
import com.stars.entity.dto.sys.SystemUserAppOrgDTO;
import com.stars.entity.dto.sys.SystemUserDTO;
import com.stars.entity.sys.SystemUser;
import com.stars.services.system.UserAccessService;
import com.stars.services.system.UserConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author : dj
 * @version ：1.0.0
 * @Date : create by 2018/12/4 18:25
 * @description ：用户增删改控制层
 * @note 注意事项
 */
@RestController
@RequestMapping("/userCtl")
@Api(tags = "用户增删改控制层")
public class UserController extends BaseController {
    @Autowired
    private UserConfigService userConfigServiceImpl;
    @Autowired
    private UserAccessService userAccessServiceImpl;

    /**
     * 添加用户接口
     *
     * @param systemUser
     * @return
     * @throws Exception
     */
    @PostMapping("/userInsert")
    @ApiOperation(value = "添加用户接口")
    @Log(detail = BaseMsg.MODULAR_ADD+"添加用户接口",operationType = OperationType.INSERT)
    public ResultMsg userInsert(@Validated({AddView.class}) SystemUser systemUser, String [] roleIds, HttpServletRequest request) throws Exception {
        setSystemAppExcludesUser(systemUser, request);
        return userConfigServiceImpl.userInsert(systemUser, roleIds);

    }
    /**
     * 修改用户接口
     *
     * @param systemUser
     * @return
     * @throws Exception
     */
    @PostMapping("/userUpdate")
    @ApiOperation(value = "修改用户接口")
    @UserMenuUpdateRedis(permission = true)
    public ResultMsg userUpdate(@Validated({UpdateView.class}) SystemUser systemUser, String [] roleIds, HttpServletRequest request) throws Exception {
        setUpdateBase(systemUser,request);
        return userConfigServiceImpl.userUpdate(systemUser, roleIds);
    }


    /**
     * 根据用户id查询用户对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/userQueryById")
    @ApiOperation(value = "根据用户id查询用户对象")
    public ResultMsg userQueryById(String id) throws Exception {
        SystemUserAppOrgDTO systemUser = userAccessServiceImpl.userQueryById(id);
        if (systemUser == null) {
            return ResultMsg.customMsg(BaseCode.USER_NOT_EXISTED, BaseMsg.USER_NOT_EXISTED, null);
        }
        return ResultMsg.success(systemUser);
    }

    /**
     * 用户分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @GetMapping("/userQueryByList")
    @ApiOperation(value = "用户分页查询")
    public PageInfo<SystemUserDTO> userQueryByList(Integer pageNum, Integer pageSize, SystemUserDTO command) throws Exception {
        return userAccessServiceImpl.findByPage(pageNum, pageSize, command);
    }
    /**
     * 用户分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @GetMapping("/pageList")
    @ApiOperation(value = "用户分页查询")
    public PageInfo<SystemUserAppOrgDTO> pageList(Integer pageNum, Integer pageSize, SystemUserDTO command) throws Exception {
        return userAccessServiceImpl.findByRolePage(pageNum, pageSize, command);
    }

    /**
     * 根据id删除用户
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @PostMapping("/userDelete")
    @ApiOperation(value = "根据id删除用户")
    public ResultMsg userDelete(String ids) throws Exception {
        return userConfigServiceImpl.deleteEntity(ids);
    }

    /**
     * 修改用户接口
     *
     * @param systemUser
     * @return
     * @throws Exception
     */
    @PostMapping("/changePassword")
    @ResponseBody
    public ResultMsg changePassword(@Validated({UpdateView.class}) SystemUser systemUser,String oldPass,HttpServletRequest request) throws Exception {
        SystemUser currentUser = getCurrentUser(request);
        if (systemUser == null) {
            return ResultMsg.customMsg(BaseCode.VLIDATE_PARM, "帐号或者密码错误", "帐号或者密码错误");
        }
        String oldPassword = MD5Util.sign(oldPass,false,currentUser.getUserSalt(),currentUser.getUserSalt2());
        if(!oldPassword.equals(currentUser.getUserPassword())){
            return ResultMsg.customMsg(BaseCode.VLIDATE_PARM, "帐号或者密码错误", "帐号或者密码错误");
        }
        String password = MD5Util.sign(systemUser.getUserPassword(),false,currentUser.getUserSalt(),currentUser.getUserSalt2());
        currentUser.setUserPassword(password);
        return userConfigServiceImpl.changePassword(currentUser);
    }


}
